{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bertopic import BERTopic\n",
    "import os\n",
    "os.environ[\"CUDA_DEVICE_ORDER\"]=\"PCI_BUS_ID\" \n",
    "os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"0\" "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "31920"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "file1 = open('../datasets/Amazon-531/llama_label_50.txt', 'r')\n",
    "documents = file1.readlines()[:31920]\n",
    "len(documents)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "abstracts = []\n",
    "for row in documents:\n",
    "    row_list = row.strip().split(\" \")[1:]\n",
    "    newrow = \" \".join(row_list)\n",
    "    abstracts.append(newrow)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b91b40f8f4af4eda8e981212896b5042",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Batches:   0%|          | 0/998 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from sentence_transformers import SentenceTransformer\n",
    "\n",
    "# Pre-calculate embeddings\n",
    "embedding_model = SentenceTransformer(\"all-MiniLM-L6-v2\")\n",
    "embeddings = embedding_model.encode(abstracts, show_progress_bar=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from umap import UMAP\n",
    "\n",
    "umap_model = UMAP(n_neighbors=5, n_components=5, min_dist=0.0, metric='cosine', random_state=42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from hdbscan import HDBSCAN\n",
    "\n",
    "hdbscan_model = HDBSCAN(min_cluster_size=20, metric='euclidean', cluster_selection_method='eom', prediction_data=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.feature_extraction.text import CountVectorizer\n",
    "vectorizer_model = CountVectorizer(stop_words=\"english\", min_df=2, ngram_range=(1, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_id = 'meta-llama/Llama-2-13b-chat-hf'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "from torch import bfloat16\n",
    "import transformers\n",
    "\n",
    "# set quantization configuration to load large model with less GPU memory\n",
    "# this requires the `bitsandbytes` library\n",
    "\n",
    "bnb_config = transformers.BitsAndBytesConfig(\n",
    "    load_in_4bit=True,  # 4-bit quantization\n",
    "    bnb_4bit_quant_type='nf4',  # Normalized float 4\n",
    "    bnb_4bit_use_double_quant=True,  # Second quantization after the first\n",
    "    bnb_4bit_compute_dtype=bfloat16  # Computation type\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
      "To disable this warning, you can either:\n",
      "\t- Avoid using `tokenizers` before the fork if possible\n",
      "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n",
      "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
      "To disable this warning, you can either:\n",
      "\t- Avoid using `tokenizers` before the fork if possible\n",
      "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n",
      "The model weights are not tied. Please use the `tie_weights` method before using the `infer_auto_device` function.\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "5549a9cafbdc4b7f9770ff381da8a4c4",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Loading checkpoint shards:   0%|          | 0/3 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "LlamaForCausalLM(\n",
       "  (model): LlamaModel(\n",
       "    (embed_tokens): Embedding(32000, 5120)\n",
       "    (layers): ModuleList(\n",
       "      (0-39): 40 x LlamaDecoderLayer(\n",
       "        (self_attn): LlamaAttention(\n",
       "          (q_proj): Linear4bit(in_features=5120, out_features=5120, bias=False)\n",
       "          (k_proj): Linear4bit(in_features=5120, out_features=5120, bias=False)\n",
       "          (v_proj): Linear4bit(in_features=5120, out_features=5120, bias=False)\n",
       "          (o_proj): Linear4bit(in_features=5120, out_features=5120, bias=False)\n",
       "          (rotary_emb): LlamaRotaryEmbedding()\n",
       "        )\n",
       "        (mlp): LlamaMLP(\n",
       "          (gate_proj): Linear4bit(in_features=5120, out_features=13824, bias=False)\n",
       "          (up_proj): Linear4bit(in_features=5120, out_features=13824, bias=False)\n",
       "          (down_proj): Linear4bit(in_features=13824, out_features=5120, bias=False)\n",
       "          (act_fn): SiLUActivation()\n",
       "        )\n",
       "        (input_layernorm): LlamaRMSNorm()\n",
       "        (post_attention_layernorm): LlamaRMSNorm()\n",
       "      )\n",
       "    )\n",
       "    (norm): LlamaRMSNorm()\n",
       "  )\n",
       "  (lm_head): Linear(in_features=5120, out_features=32000, bias=False)\n",
       ")"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Llama 2 Tokenizer\n",
    "tokenizer = transformers.AutoTokenizer.from_pretrained(model_id)\n",
    "\n",
    "# Llama 2 Model\n",
    "model = transformers.AutoModelForCausalLM.from_pretrained(\n",
    "    model_id,\n",
    "    trust_remote_code=True,\n",
    "    quantization_config=bnb_config,\n",
    "    device_map='auto',\n",
    ")\n",
    "model.eval()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Our text generator\n",
    "generator = transformers.pipeline(\n",
    "    model=model, tokenizer=tokenizer,\n",
    "    task='text-generation',\n",
    "    temperature=0.1,\n",
    "    max_new_tokens=500,\n",
    "    repetition_penalty=1.1\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# System prompt describes information given to all conversations\n",
    "system_prompt = \"\"\"\n",
    "<s>[INST] <<SYS>>\n",
    "You are a helpful, respectful and honest assistant for labeling topics.\n",
    "<</SYS>>\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Example prompt demonstrating the output we are looking for\n",
    "example_prompt = \"\"\"\n",
    "I have a topic that contains the following documents:\n",
    "- Traditional diets in most cultures were primarily plant-based with a little meat on top, but with the rise of industrial style meat production and factory farming, meat has become a staple food.\n",
    "- Meat, but especially beef, is the word food in terms of emissions.\n",
    "- Eating meat doesn't make you a bad person, not eating meat doesn't make you a good one.\n",
    "\n",
    "The topic is described by the following keywords: 'meat, beef, eat, eating, emissions, steak, food, health, processed, chicken'.\n",
    "\n",
    "Based on the information about the topic above, please create a short label of this topic. Make sure you to only return the label and nothing more.\n",
    "\n",
    "[/INST] Environmental impacts of eating meat\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Our main prompt with documents ([DOCUMENTS]) and keywords ([KEYWORDS]) tags\n",
    "main_prompt = \"\"\"\n",
    "[INST]\n",
    "I have a topic that contains the following documents:\n",
    "[DOCUMENTS]\n",
    "\n",
    "The topic is described by the following keywords: '[KEYWORDS]'.\n",
    "\n",
    "Based on the information about the topic above, please create a short label of this topic. Make sure you to only return the label and nothing more.\n",
    "[/INST]\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = system_prompt + example_prompt + main_prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bertopic.representation import TextGeneration\n",
    "\n",
    "\n",
    "# Text generation with Llama 2\n",
    "llama2 = TextGeneration(generator, prompt=prompt)\n",
    "\n",
    "# All representation models\n",
    "representation_model = {\n",
    "    \"Llama2\": llama2,\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2024-01-18 01:30:57,985 - BERTopic - Dimensionality - Fitting the dimensionality reduction algorithm\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2024-01-18 01:31:09,283 - BERTopic - Dimensionality - Completed ✓\n",
      "2024-01-18 01:31:09,286 - BERTopic - Cluster - Start clustering the reduced embeddings\n",
      "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
      "To disable this warning, you can either:\n",
      "\t- Avoid using `tokenizers` before the fork if possible\n",
      "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n",
      "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
      "To disable this warning, you can either:\n",
      "\t- Avoid using `tokenizers` before the fork if possible\n",
      "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n",
      "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
      "To disable this warning, you can either:\n",
      "\t- Avoid using `tokenizers` before the fork if possible\n",
      "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n",
      "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
      "To disable this warning, you can either:\n",
      "\t- Avoid using `tokenizers` before the fork if possible\n",
      "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n",
      "2024-01-18 01:33:36,897 - BERTopic - Cluster - Completed ✓\n",
      "2024-01-18 01:33:36,910 - BERTopic - Representation - Extracting topics from clusters using representation models.\n",
      "100%|██████████| 398/398 [08:53<00:00,  1.34s/it]\n",
      "2024-01-18 01:42:31,551 - BERTopic - Representation - Completed ✓\n"
     ]
    }
   ],
   "source": [
    "topic_model = BERTopic(\n",
    "\n",
    "  # Sub-models\n",
    "  embedding_model=embedding_model,\n",
    "  umap_model=umap_model,\n",
    "  hdbscan_model=hdbscan_model,\n",
    "  vectorizer_model=vectorizer_model,\n",
    "  representation_model=representation_model,\n",
    "\n",
    "  # Hyperparameters\n",
    "  top_n_words=10,\n",
    "  verbose=True,\n",
    "  calculate_probabilities=True,\n",
    ")\n",
    "\n",
    "# Train model\n",
    "topics, probs = topic_model.fit_transform(abstracts, embeddings)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Topic</th>\n",
       "      <th>Count</th>\n",
       "      <th>Name</th>\n",
       "      <th>Representation</th>\n",
       "      <th>Llama2</th>\n",
       "      <th>Representative_Docs</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-1</td>\n",
       "      <td>9181</td>\n",
       "      <td>-1_toys_dolls_customer_service_food_ingredient...</td>\n",
       "      <td>[toys_dolls, customer_service, food_ingredient...</td>\n",
       "      <td>[Consumer goods and services, , , , , , , , , ]</td>\n",
       "      <td>[customer_service, online_shopping, frustratio...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>1141</td>\n",
       "      <td>0_home_furnishings_decor_home_furniture_decor_...</td>\n",
       "      <td>[home_furnishings_decor, home_furniture_decor,...</td>\n",
       "      <td>[Home Furnishings and Baby Products, , , , , ,...</td>\n",
       "      <td>[furniture_baby_kids, home_furnishings_decor, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>779</td>\n",
       "      <td>1_fragrances_perfumes_fragrances_fashion_fragr...</td>\n",
       "      <td>[fragrances_perfumes, fragrances, fashion_frag...</td>\n",
       "      <td>[Fashion Fragrances, , , , , , , , , ]</td>\n",
       "      <td>[beauty_personal_care, fragrances_perfumes, ta...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>438</td>\n",
       "      <td>2_baby_gear_accessories_baby_gear_equipment_pa...</td>\n",
       "      <td>[baby_gear_accessories, baby_gear_equipment, p...</td>\n",
       "      <td>[Parenting Essentials, , , , , , , , , ]</td>\n",
       "      <td>[parenting_baby_care, household_safety_product...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>3</td>\n",
       "      <td>332</td>\n",
       "      <td>3_pet_toys_dog_toys_pet_supplies toys_games_pe...</td>\n",
       "      <td>[pet_toys, dog_toys, pet_supplies toys_games, ...</td>\n",
       "      <td>[Pet Toys and Accessories, , , , , , , , , ]</td>\n",
       "      <td>[pet_toys, dog_toys, interactive_toys, pet_toy...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>393</th>\n",
       "      <td>392</td>\n",
       "      <td>20</td>\n",
       "      <td>392_food_vegetarian_food_vegan_vegetarian_food...</td>\n",
       "      <td>[food_vegetarian, food_vegan, vegetarian_food,...</td>\n",
       "      <td>[Vegetarian and Vegan Food and Recipes, , , , ...</td>\n",
       "      <td>[food_vegetarian, grocery_pantry_staples, tast...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>394</th>\n",
       "      <td>393</td>\n",
       "      <td>20</td>\n",
       "      <td>393_sexual_wellness_adult_products_personal_ca...</td>\n",
       "      <td>[sexual_wellness, adult_products, personal_car...</td>\n",
       "      <td>[Adult Products and Sexual Wellness, , , , , ,...</td>\n",
       "      <td>[adult_products, sexual_wellness, personal_car...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>395</th>\n",
       "      <td>394</td>\n",
       "      <td>20</td>\n",
       "      <td>394_cleaning_supplies home_maintenance_home_ma...</td>\n",
       "      <td>[cleaning_supplies home_maintenance, home_main...</td>\n",
       "      <td>[Home maintenance and cleaning supplies, , , ,...</td>\n",
       "      <td>[home_maintenance, cleaning_supplies, dvd_vide...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>396</th>\n",
       "      <td>395</td>\n",
       "      <td>20</td>\n",
       "      <td>395_entertainment leisure_activities_leisure_a...</td>\n",
       "      <td>[entertainment leisure_activities, leisure_act...</td>\n",
       "      <td>[Recreational gaming and social activities, , ...</td>\n",
       "      <td>[games, entertainment, leisure_activities, gam...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>397</th>\n",
       "      <td>396</td>\n",
       "      <td>20</td>\n",
       "      <td>396_pet_supplies consumer_goods_pet_food_retai...</td>\n",
       "      <td>[pet_supplies consumer_goods, pet_food, retail...</td>\n",
       "      <td>[Pet care and supplies, , , , , , , , , ]</td>\n",
       "      <td>[retail_pet_supplies, shopping_pets, animal_ca...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>398 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     Topic  Count                                               Name  \\\n",
       "0       -1   9181  -1_toys_dolls_customer_service_food_ingredient...   \n",
       "1        0   1141  0_home_furnishings_decor_home_furniture_decor_...   \n",
       "2        1    779  1_fragrances_perfumes_fragrances_fashion_fragr...   \n",
       "3        2    438  2_baby_gear_accessories_baby_gear_equipment_pa...   \n",
       "4        3    332  3_pet_toys_dog_toys_pet_supplies toys_games_pe...   \n",
       "..     ...    ...                                                ...   \n",
       "393    392     20  392_food_vegetarian_food_vegan_vegetarian_food...   \n",
       "394    393     20  393_sexual_wellness_adult_products_personal_ca...   \n",
       "395    394     20  394_cleaning_supplies home_maintenance_home_ma...   \n",
       "396    395     20  395_entertainment leisure_activities_leisure_a...   \n",
       "397    396     20  396_pet_supplies consumer_goods_pet_food_retai...   \n",
       "\n",
       "                                        Representation  \\\n",
       "0    [toys_dolls, customer_service, food_ingredient...   \n",
       "1    [home_furnishings_decor, home_furniture_decor,...   \n",
       "2    [fragrances_perfumes, fragrances, fashion_frag...   \n",
       "3    [baby_gear_accessories, baby_gear_equipment, p...   \n",
       "4    [pet_toys, dog_toys, pet_supplies toys_games, ...   \n",
       "..                                                 ...   \n",
       "393  [food_vegetarian, food_vegan, vegetarian_food,...   \n",
       "394  [sexual_wellness, adult_products, personal_car...   \n",
       "395  [cleaning_supplies home_maintenance, home_main...   \n",
       "396  [entertainment leisure_activities, leisure_act...   \n",
       "397  [pet_supplies consumer_goods, pet_food, retail...   \n",
       "\n",
       "                                                Llama2  \\\n",
       "0      [Consumer goods and services, , , , , , , , , ]   \n",
       "1    [Home Furnishings and Baby Products, , , , , ,...   \n",
       "2               [Fashion Fragrances, , , , , , , , , ]   \n",
       "3             [Parenting Essentials, , , , , , , , , ]   \n",
       "4         [Pet Toys and Accessories, , , , , , , , , ]   \n",
       "..                                                 ...   \n",
       "393  [Vegetarian and Vegan Food and Recipes, , , , ...   \n",
       "394  [Adult Products and Sexual Wellness, , , , , ,...   \n",
       "395  [Home maintenance and cleaning supplies, , , ,...   \n",
       "396  [Recreational gaming and social activities, , ...   \n",
       "397          [Pet care and supplies, , , , , , , , , ]   \n",
       "\n",
       "                                   Representative_Docs  \n",
       "0    [customer_service, online_shopping, frustratio...  \n",
       "1    [furniture_baby_kids, home_furnishings_decor, ...  \n",
       "2    [beauty_personal_care, fragrances_perfumes, ta...  \n",
       "3    [parenting_baby_care, household_safety_product...  \n",
       "4    [pet_toys, dog_toys, interactive_toys, pet_toy...  \n",
       "..                                                 ...  \n",
       "393  [food_vegetarian, grocery_pantry_staples, tast...  \n",
       "394  [adult_products, sexual_wellness, personal_car...  \n",
       "395  [home_maintenance, cleaning_supplies, dvd_vide...  \n",
       "396  [games, entertainment, leisure_activities, gam...  \n",
       "397  [retail_pet_supplies, shopping_pets, animal_ca...  \n",
       "\n",
       "[398 rows x 6 columns]"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "topic_model.get_topic_info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = topic_model.get_topic_info()\n",
    "df.to_csv(\"../datasets/Amazon-531/bertopic_result/50topic_info_14000.csv\", index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Document</th>\n",
       "      <th>Topic</th>\n",
       "      <th>Name</th>\n",
       "      <th>Representation</th>\n",
       "      <th>Llama2</th>\n",
       "      <th>Representative_Docs</th>\n",
       "      <th>Top_n_words</th>\n",
       "      <th>Probability</th>\n",
       "      <th>Representative_document</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0: health_personal_care, medical_supplies_equi...</td>\n",
       "      <td>34</td>\n",
       "      <td>34_health_monitors_medical_supplies_equipment ...</td>\n",
       "      <td>[health_monitors, medical_supplies_equipment h...</td>\n",
       "      <td>[Wearable Health Technology, , , , , , , , , ]</td>\n",
       "      <td>[health_personal_care, medical_supplies_equipm...</td>\n",
       "      <td>health_monitors - medical_supplies_equipment h...</td>\n",
       "      <td>0.067349</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1: food_beverages, dietary_supplements, packag...</td>\n",
       "      <td>-1</td>\n",
       "      <td>-1_toys_dolls_customer_service_food_ingredient...</td>\n",
       "      <td>[toys_dolls, customer_service, food_ingredient...</td>\n",
       "      <td>[Consumer goods and services, , , , , , , , , ]</td>\n",
       "      <td>[customer_service, online_shopping, frustratio...</td>\n",
       "      <td>toys_dolls - customer_service - food_ingredien...</td>\n",
       "      <td>0.700633</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2: food_snacks, shopping_retail, coffee\\n</td>\n",
       "      <td>-1</td>\n",
       "      <td>-1_toys_dolls_customer_service_food_ingredient...</td>\n",
       "      <td>[toys_dolls, customer_service, food_ingredient...</td>\n",
       "      <td>[Consumer goods and services, , , , , , , , , ]</td>\n",
       "      <td>[customer_service, online_shopping, frustratio...</td>\n",
       "      <td>toys_dolls - customer_service - food_ingredien...</td>\n",
       "      <td>0.036208</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2: shopping_retail, food_beverages, online_sho...</td>\n",
       "      <td>55</td>\n",
       "      <td>55_online_shopping food_snacks_online_shopping...</td>\n",
       "      <td>[online_shopping food_snacks, online_shopping,...</td>\n",
       "      <td>[Online shopping for food and candy, , , , , ,...</td>\n",
       "      <td>[food_candy, shopping_retail, customer_service...</td>\n",
       "      <td>online_shopping food_snacks - online_shopping ...</td>\n",
       "      <td>0.121055</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2: shipping_delivery, logistics_supply_chain, ...</td>\n",
       "      <td>182</td>\n",
       "      <td>182_shipping_delivery_customer_service shippin...</td>\n",
       "      <td>[shipping_delivery, customer_service shipping_...</td>\n",
       "      <td>[E-commerce logistics and customer service, , ...</td>\n",
       "      <td>[customer_service, shipping_delivery, online_s...</td>\n",
       "      <td>shipping_delivery - customer_service shipping_...</td>\n",
       "      <td>0.232849</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31915</th>\n",
       "      <td>13998: weight_loss, health_personal_care, diet...</td>\n",
       "      <td>161</td>\n",
       "      <td>161_weight_loss_dietary_supplements_weight_los...</td>\n",
       "      <td>[weight_loss, dietary_supplements, weight_loss...</td>\n",
       "      <td>[Weight Loss Supplements and Nutrition, , , , ...</td>\n",
       "      <td>[weight_loss, dietary_supplements, appetite_su...</td>\n",
       "      <td>weight_loss - dietary_supplements - weight_los...</td>\n",
       "      <td>0.743971</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31916</th>\n",
       "      <td>13999: toys_games, lego, movies_tv_shows_juras...</td>\n",
       "      <td>112</td>\n",
       "      <td>112_toys_action_figures_star_wars_action_figur...</td>\n",
       "      <td>[toys_action_figures, star_wars, action_figure...</td>\n",
       "      <td>[Action figures and collectibles from popular ...</td>\n",
       "      <td>[toys_action_figures, entertainment_marvel, co...</td>\n",
       "      <td>toys_action_figures - star_wars - action_figur...</td>\n",
       "      <td>0.240691</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31917</th>\n",
       "      <td>13999: toys_games_puzzles, building_constructi...</td>\n",
       "      <td>374</td>\n",
       "      <td>374_toys_games_puzzles building_sets_building_...</td>\n",
       "      <td>[toys_games_puzzles building_sets, building_se...</td>\n",
       "      <td>[Building and Educational Toys, , , , , , , , , ]</td>\n",
       "      <td>[toys_games_puzzles, building_sets, educationa...</td>\n",
       "      <td>toys_games_puzzles building_sets - building_se...</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31918</th>\n",
       "      <td>14000: toys_kids, musical_instruments, active_...</td>\n",
       "      <td>246</td>\n",
       "      <td>246_musical_instruments_music_instruments_toys...</td>\n",
       "      <td>[musical_instruments, music_instruments, toys_...</td>\n",
       "      <td>[Kids' Musical Interests, , , , , , , , , ]</td>\n",
       "      <td>[toys_kids, musical_instruments, entertainment...</td>\n",
       "      <td>musical_instruments - music_instruments - toys...</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31919</th>\n",
       "      <td>14000: toys_kids, safety_products, music_equip...</td>\n",
       "      <td>-1</td>\n",
       "      <td>-1_toys_dolls_customer_service_food_ingredient...</td>\n",
       "      <td>[toys_dolls, customer_service, food_ingredient...</td>\n",
       "      <td>[Consumer goods and services, , , , , , , , , ]</td>\n",
       "      <td>[customer_service, online_shopping, frustratio...</td>\n",
       "      <td>toys_dolls - customer_service - food_ingredien...</td>\n",
       "      <td>0.454929</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>31920 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                Document  Topic  \\\n",
       "0      0: health_personal_care, medical_supplies_equi...     34   \n",
       "1      1: food_beverages, dietary_supplements, packag...     -1   \n",
       "2              2: food_snacks, shopping_retail, coffee\\n     -1   \n",
       "3      2: shopping_retail, food_beverages, online_sho...     55   \n",
       "4      2: shipping_delivery, logistics_supply_chain, ...    182   \n",
       "...                                                  ...    ...   \n",
       "31915  13998: weight_loss, health_personal_care, diet...    161   \n",
       "31916  13999: toys_games, lego, movies_tv_shows_juras...    112   \n",
       "31917  13999: toys_games_puzzles, building_constructi...    374   \n",
       "31918  14000: toys_kids, musical_instruments, active_...    246   \n",
       "31919  14000: toys_kids, safety_products, music_equip...     -1   \n",
       "\n",
       "                                                    Name  \\\n",
       "0      34_health_monitors_medical_supplies_equipment ...   \n",
       "1      -1_toys_dolls_customer_service_food_ingredient...   \n",
       "2      -1_toys_dolls_customer_service_food_ingredient...   \n",
       "3      55_online_shopping food_snacks_online_shopping...   \n",
       "4      182_shipping_delivery_customer_service shippin...   \n",
       "...                                                  ...   \n",
       "31915  161_weight_loss_dietary_supplements_weight_los...   \n",
       "31916  112_toys_action_figures_star_wars_action_figur...   \n",
       "31917  374_toys_games_puzzles building_sets_building_...   \n",
       "31918  246_musical_instruments_music_instruments_toys...   \n",
       "31919  -1_toys_dolls_customer_service_food_ingredient...   \n",
       "\n",
       "                                          Representation  \\\n",
       "0      [health_monitors, medical_supplies_equipment h...   \n",
       "1      [toys_dolls, customer_service, food_ingredient...   \n",
       "2      [toys_dolls, customer_service, food_ingredient...   \n",
       "3      [online_shopping food_snacks, online_shopping,...   \n",
       "4      [shipping_delivery, customer_service shipping_...   \n",
       "...                                                  ...   \n",
       "31915  [weight_loss, dietary_supplements, weight_loss...   \n",
       "31916  [toys_action_figures, star_wars, action_figure...   \n",
       "31917  [toys_games_puzzles building_sets, building_se...   \n",
       "31918  [musical_instruments, music_instruments, toys_...   \n",
       "31919  [toys_dolls, customer_service, food_ingredient...   \n",
       "\n",
       "                                                  Llama2  \\\n",
       "0         [Wearable Health Technology, , , , , , , , , ]   \n",
       "1        [Consumer goods and services, , , , , , , , , ]   \n",
       "2        [Consumer goods and services, , , , , , , , , ]   \n",
       "3      [Online shopping for food and candy, , , , , ,...   \n",
       "4      [E-commerce logistics and customer service, , ...   \n",
       "...                                                  ...   \n",
       "31915  [Weight Loss Supplements and Nutrition, , , , ...   \n",
       "31916  [Action figures and collectibles from popular ...   \n",
       "31917  [Building and Educational Toys, , , , , , , , , ]   \n",
       "31918        [Kids' Musical Interests, , , , , , , , , ]   \n",
       "31919    [Consumer goods and services, , , , , , , , , ]   \n",
       "\n",
       "                                     Representative_Docs  \\\n",
       "0      [health_personal_care, medical_supplies_equipm...   \n",
       "1      [customer_service, online_shopping, frustratio...   \n",
       "2      [customer_service, online_shopping, frustratio...   \n",
       "3      [food_candy, shopping_retail, customer_service...   \n",
       "4      [customer_service, shipping_delivery, online_s...   \n",
       "...                                                  ...   \n",
       "31915  [weight_loss, dietary_supplements, appetite_su...   \n",
       "31916  [toys_action_figures, entertainment_marvel, co...   \n",
       "31917  [toys_games_puzzles, building_sets, educationa...   \n",
       "31918  [toys_kids, musical_instruments, entertainment...   \n",
       "31919  [customer_service, online_shopping, frustratio...   \n",
       "\n",
       "                                             Top_n_words  Probability  \\\n",
       "0      health_monitors - medical_supplies_equipment h...     0.067349   \n",
       "1      toys_dolls - customer_service - food_ingredien...     0.700633   \n",
       "2      toys_dolls - customer_service - food_ingredien...     0.036208   \n",
       "3      online_shopping food_snacks - online_shopping ...     0.121055   \n",
       "4      shipping_delivery - customer_service shipping_...     0.232849   \n",
       "...                                                  ...          ...   \n",
       "31915  weight_loss - dietary_supplements - weight_los...     0.743971   \n",
       "31916  toys_action_figures - star_wars - action_figur...     0.240691   \n",
       "31917  toys_games_puzzles building_sets - building_se...     1.000000   \n",
       "31918  musical_instruments - music_instruments - toys...     1.000000   \n",
       "31919  toys_dolls - customer_service - food_ingredien...     0.454929   \n",
       "\n",
       "       Representative_document  \n",
       "0                        False  \n",
       "1                        False  \n",
       "2                        False  \n",
       "3                        False  \n",
       "4                        False  \n",
       "...                        ...  \n",
       "31915                    False  \n",
       "31916                    False  \n",
       "31917                    False  \n",
       "31918                    False  \n",
       "31919                    False  \n",
       "\n",
       "[31920 rows x 9 columns]"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "topic_model.get_document_info(documents)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = topic_model.get_document_info(documents)\n",
    "df.to_csv(\"../datasets/Amazon-531/bertopic_result/50label_predict_14000.csv\", index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "with open('../datasets/Amazon-531/bertopic_result/50label_prob_14000.npy', 'wb') as f:\n",
    "    np.save(f, probs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Multi-Label",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
